package cn.cloudevops.mapdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.AMapOptions;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;

public class LocationActivity extends AppCompatActivity {

    private MapView mapView;
    private AMap aMap;
    private LinearLayout.LayoutParams mParams;
    private RelativeLayout mContainerLayout;
    private String markerTitle;
    private String markerDetail;
    private LatLng point;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);

        mContainerLayout = (RelativeLayout) findViewById(R.id.activity_location);
        AMapOptions aOptions = new AMapOptions();
        // 获取另一个页面传过来的值

        markerTitle = getIntent().getStringExtra(MainActivity.MARKER_TITLE);
        markerDetail = getIntent().getStringExtra(MainActivity.MARKER_DETAIL);
        double lat = getIntent().getDoubleExtra(MainActivity.LATITUDE, 39.904989);
        double lng = getIntent().getDoubleExtra(MainActivity.LONGITUDE, 116.405285);
        point = new LatLng(lat,lng);

        // 第二个参数是缩放比例
        aOptions.camera(new CameraPosition(point, 19f, 0, 0));

        mapView = new MapView(this, aOptions);
        mapView.onCreate(savedInstanceState);
        mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        mContainerLayout.addView(mapView, mParams);
        aMap = mapView.getMap();
        aMap.addMarker(new MarkerOptions().position(point).title(markerTitle).snippet(markerDetail));
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
}
